﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;

namespace SynoManager.FileStation.Tasks
{
    public abstract class TaskBase
    {
        private TaskQueue queue;
        private TaskState state = TaskState.Pending;
        private double progress;

        public TaskQueue Queue
        {
            get { return queue; }
            internal set { queue = value; }
        }

        public TaskState State
        {
            get { return state; }
            internal set { state = value; }
        }

        public abstract void Execute();

        public abstract string DisplayTitle { get;}
        public abstract string DisplayDescription { get; }
        public abstract Image DisplayImage { get;}

        public double Progress
        {
            get { return progress; }
        }

        public event TaskProgressDelegate ProgressChanged;
        protected void OnProgressChanged(double percentageComplete)
        {
            if (ProgressChanged != null)
                ProgressChanged(this, percentageComplete);
            progress = percentageComplete;
            queue.UpdateProgress(this, percentageComplete);
        }

        public event EventHandler Completed;
        protected void OnCompleted()
        {
            State = TaskState.Completed;
            progress = 100;

            if (Completed != null)
                Completed(this, EventArgs.Empty);

            queue.SetCompleted(this);
        }
    }

    public delegate void TaskProgressDelegate(TaskBase task, double percentageComplete);
 
    public enum TaskState
    {
        Pending,
        Executing,
        Completed
    }
}
